import axios from 'axios'
import { ElMessage } from 'element-plus'

// axios响应式拦截器
axios.interceptors.response.use(res => res.data, e => {
    // 统一错误提示
    if (e.response && e.response.status !== 200) {
        ElMessage({
            type: 'warning',
            message: e.response.data.message
        })
    }
    return Promise.reject(e)
})

const post = (url, data) => {
    return axios({
        method: 'post',
        url,
        data: data,
        timeout: 5000,
        headers: {
            'X-Requested-With': 'XMLHttpRequest'
        }
    })
}

const get = (url, params) => {
    return axios({
        method: 'get',
        url,
        params, // get 请求时带的参数
        timeout: 5000,
        headers: {
            'X-Requested-With': 'XMLHttpRequest',
        }
    })
}

const put = (url, data) => {
    return axios({
        method: 'put',
        url,
        data: data,
        timeout: 5000,
        headers: {
            'Content-Type': 'application/json;charset=utf-8'
        }
    })
}

const del = (url, data) => {
    return axios({
        method: 'delete',
        url,
        data: data,
        timeout: 5000,
        headers: {
            'Content-Type': 'application/json;charset=utf-8'
        }
    })
}

export default {
    post, get, put, del
}